package com.itheima.health.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.health.entity.Result;
import com.itheima.health.mapper.MemberMapper;
import com.itheima.health.mapper.OrderMapper;
import com.itheima.health.pojo.MemOrderDto;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private MemberMapper memberMapper;

    @Override
    public Result submit(MemOrderDto memOrderDto) {
        //todo 这里需要根据手机号或者身份证号判断的是预约人是否已经预约
        //若已经预约,告诉用户已经预约
        //未预约,将传进来的用户信息进行保存,返回预约成功
        Member member = new Member();
        member.setId(memOrderDto.getMemberId());

        LambdaQueryWrapper<Member> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Member::getIdCard,memOrderDto.getIdCard());
        Member member2 = memberMapper.selectOne(wrapper);
        if(member2==null){
            //该身份证号暂未有预约信息,添加信息后返回预约成功
            String s = JSON.toJSONString(memOrderDto);
            Order order1 = JSON.parseObject(s, Order.class);
            Member member1 = JSON.parseObject(s, Member.class);
            orderMapper.insert(order1);
            memberMapper.insert(member1);
            return new Result(true,"预约成功");
        }
        //该身份证已经预约,可以告诉用户去修改页面修改套餐或者其他信息
        return new Result(false,"您已预约过,若要修改信息请到修改页面");
    }
}
